﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using DevExpress.Web.ASPxUploadControl;

public partial class Fotos : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((Request.QueryString["ID"] == null)
                || (Request.QueryString["ID"] == ""))
            UploadControl.Enabled = false;
        else
            UploadControl.Enabled = true;
    }
    protected void UploadControl_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e)
    {        
        try
        {            
            e.CallbackData = SavePostedFiles(e.UploadedFile);
        }
        catch (Exception ex)
        {
            e.IsValid = false;
            e.ErrorText = ex.Message;
        }
    }
    #region Metodos de subir archivos
    const string UploadDirectory = "~/Fotos/";
    protected string SavePostedFile(UploadedFile uploadedFile)
    {
        string ret = "";
        if (uploadedFile.IsValid)
        {
            //string tempFileName = MapPath(UploadDirectory) + Path.GetRandomFileName();
            //string fileName = "ThumbnailImage.jpg";
            //string resFileName = MapPath(UploadDirectory) + fileName;
            //uploadedFile.SaveAs(tempFileName);

            //PhotoUtils.CreateThumbnailFile(tempFileName, ThumbnailSize, resFileName);

            //File.Delete(tempFileName);
            //ret = fileName;
            string _ruta = ObtenerRutaPorID();
            ret = _ruta + "\\" + uploadedFile.FileName.Replace(" ", "_"); ;//reemplazo espacios por guiones bajos            
            uploadedFile.SaveAs(ret);


        }
        return ret;
    }
    /// <summary>
    /// Devuelve la ruta del presupuesto.
    /// Si el directorio no existe, lo crea.
    /// </summary>
    private string ObtenerRutaPorID()
    {
        string _carpeta = MapPath("~/Fotos") + "\\" + Request.QueryString["ID"];
        //Directory.GetCurrentDirectory()
        if (Directory.Exists(_carpeta) == false)
            Directory.CreateDirectory(_carpeta);
        //Directory.Delete(directoryString);

        return _carpeta;
    }
    #endregion
    protected string SavePostedFiles(UploadedFile uploadedFile)
    {
        string ret = "";
        if (uploadedFile.IsValid)
        {
            string _ruta = ObtenerRutaPorID();
            ret = _ruta + "\\" + uploadedFile.FileName.Replace(" ", "_"); ;//reemplazo espacios por guiones bajos            
            uploadedFile.SaveAs(ret);

            //FileInfo fileInfo = new FileInfo(uploadedFile.FileName);
            //string resFileName = MapPath(UploadDirectory) + fileInfo.Name;
            //uploadedFile.SaveAs(resFileName);

            //string fileLabel = fileInfo.Name;
            //string fileType = uploadedFile.PostedFile.ContentType.ToString();
            //string fileLength = uploadedFile.PostedFile.ContentLength / 1024 + "K";
            //ret = string.Format("{0} <i>({1})</i> {2}|{3}", fileLabel, fileType, fileLength, fileInfo.Name);
        }
        return ret;
    }
    protected void dsFotos_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        int _id;

        int.TryParse(Request.QueryString["ID"], out _id);
        string _carpeta = Request.PhysicalApplicationPath + "Fotos\\" + _id.ToString();
        e.InputParameters[0] = _id;
        e.InputParameters[1] = _carpeta;
    }
}
